package gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.ListSelectionModel;

import javax.swing.JComboBox;
import javax.swing.JLabel;

import model.Pallet;

@SuppressWarnings("serial")
public class Gui extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel createPalletPanel;
	private JTextField searchField;
	private JTextField createOrderTextField;
	private JLabel createOrder, createType, createLocation, createProduced, 
			createDelivered, createId, statusLabel;
	private JLabel searchOrder, searchType, searchLocation, searchProduced,
			searchDelivered, searchId;
	private JButton btnCreatePallet, btnSearch, searchBtnBlockCurrent;
	private JRadioButton rdbtnBlockedPallets, rdbtnPallets;
	private JList searchList;
	private JComboBox attributeBox;
	private DefaultListModel searchListModel;
	private DefaultComboBoxModel dayStartModel, dayEndModel, orderModel; 
	private JComboBox yearStart, monthStart, dayStart;
	private JComboBox yearEnd, monthEnd, dayEnd;
	private JLabel lblBetween;
	private JLabel lblAnd;
	private static String[] months = {"01","02","03","04","05","06","07","08","09","10","11","12"};
	private static String[] cookies = {"Almond delight","Amneris","Berliner","Nut cookie","Nut ring","Tango"};
	private JLabel lblYear;
	private JLabel label_8;
	private JLabel lblMonth;
	private JLabel label_9;
	private JLabel lblDay;
	private JLabel label_10;
	private JButton btnUnblockCurrentType;
	private JComboBox cookieComboBox, orderComboBox;
	private static final int JAN = 1, FEB =2, MAR = 3, APR =4, MAY =5,
			JUN = 6, JUL = 7, AUG =8, SEP = 9, OCT =10, NOV = 11, DEC = 12;

	/**
	 * Create the frame.
	 */
	public Gui() {
		setTitle("Pallet Manager");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 649, 666);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);

		createPalletPanel = new JPanel();
		tabbedPane.addTab("Create Pallet", null, createPalletPanel, null);
		createPalletPanel.setLayout(null);

		JLabel label_1 = new JLabel("Type:");
		label_1.setBounds(27, 85, 61, 16);
		createPalletPanel.add(label_1);

		JLabel label_5 = new JLabel("Order:");
		label_5.setBounds(27, 52, 93, 16);
		createPalletPanel.add(label_5);

		createOrderTextField = new JTextField();
		createOrderTextField.setColumns(10);
		createOrderTextField.setBounds(126, 52, 210, 28);
		createPalletPanel.add(createOrderTextField);
		
//		orderModel = new DefaultComboBoxModel();
//		orderComboBox = new JComboBox(orderModel);
//		orderComboBox.setBounds(126, 52, 210, 28);
//		createPalletPanel.add(orderComboBox);

		cookieComboBox = new JComboBox(cookies);
		cookieComboBox.setBounds(126, 85, 210, 28);
		createPalletPanel.add(cookieComboBox);
		
		JLabel label = new JLabel("Id:");
		label.setEnabled(false);
		label.setBounds(27, 172, 39, 16);
		createPalletPanel.add(label);

		JLabel label_2 = new JLabel("Type:");
		label_2.setEnabled(false);
		label_2.setBounds(27, 200, 61, 16);
		createPalletPanel.add(label_2);

		JLabel label_3 = new JLabel("Location:");
		label_3.setEnabled(false);
		label_3.setBounds(27, 228, 61, 16);
		createPalletPanel.add(label_3);

		JLabel label_4 = new JLabel("Produced:");
		label_4.setEnabled(false);
		label_4.setBounds(27, 256, 68, 16);
		createPalletPanel.add(label_4);

		JLabel label_6 = new JLabel("Delivered:");
		label_6.setEnabled(false);
		label_6.setBounds(27, 284, 78, 16);
		createPalletPanel.add(label_6);

		JLabel label_7 = new JLabel("Order:");
		label_7.setEnabled(false);
		label_7.setBounds(27, 312, 93, 16);
		createPalletPanel.add(label_7);

		createOrder = new JLabel("");
		createOrder.setEnabled(false);
		createOrder.setBounds(107, 312, 123, 16);
		createPalletPanel.add(createOrder);

		createDelivered = new JLabel("");
		createDelivered.setEnabled(false);
		createDelivered.setBounds(107, 284, 123, 16);
		createPalletPanel.add(createDelivered);

		createProduced = new JLabel("");
		createProduced.setEnabled(false);
		createProduced.setBounds(107, 256, 123, 16);
		createPalletPanel.add(createProduced);

		createLocation = new JLabel("");
		createLocation.setEnabled(false);
		createLocation.setBounds(107, 228, 123, 16);
		createPalletPanel.add(createLocation);

		createType = new JLabel("");
		createType.setEnabled(false);
		createType.setBounds(107, 200, 123, 16);
		createPalletPanel.add(createType);

		createId = new JLabel("");
		createId.setEnabled(false);
		createId.setBounds(107, 172, 123, 16);
		createPalletPanel.add(createId);

		statusLabel = new JLabel("");
		statusLabel.setBounds(96, 144, 177, 16);
		createPalletPanel.add(statusLabel);

		btnCreatePallet = new JButton("Create Pallet");
		btnCreatePallet.setBounds(367, 80, 117, 29);
		createPalletPanel.add(btnCreatePallet);

		JPanel searchPanel = new JPanel();
		tabbedPane.addTab("Search Pallet", null, searchPanel, null);
		searchPanel.setLayout(null);
		
		rdbtnBlockedPallets = new JRadioButton("Blocked Pallets");
		rdbtnBlockedPallets.setBounds(207, 106, 141, 23);
		searchPanel.add(rdbtnBlockedPallets);

		rdbtnPallets = new JRadioButton("Not blocked Pallets");
		rdbtnPallets.setSelected(true);
		rdbtnPallets.setBounds(41, 106, 167, 23);
		searchPanel.add(rdbtnPallets);
		
		ButtonGroup blockedOrNot = new ButtonGroup();
		blockedOrNot.add(rdbtnBlockedPallets);
		blockedOrNot.add(rdbtnPallets);

		searchField = new JTextField();
		searchField.setBounds(39, 76, 284, 28);
		searchPanel.add(searchField);
		searchField.setColumns(10);

		btnSearch = new JButton("Search");
		btnSearch.setBounds(335, 77, 117, 29);
		searchPanel.add(btnSearch);

		searchListModel = new DefaultListModel();
		searchList = new JList(searchListModel);
		searchList.setBounds(39, 148, 284, 380);
		searchPanel.add(searchList);
		searchList.setSelectedIndex(0);
		searchList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		//Combobox values
		String[] values = {"pallNbr" , "location" , "producedTime" , "cookieName" , "timeDelivered" , "orderNbr"};
		attributeBox = new JComboBox(values);
		attributeBox.setBounds(111, 45, 204, 23);
		searchPanel.add(attributeBox);

		JLabel lblSearchBy = new JLabel("Search by:");
		lblSearchBy.setBounds(34, 48, 78, 16);
		searchPanel.add(lblSearchBy);

		JLabel lblNbr = new JLabel("Id:");
		lblNbr.setBounds(335, 159, 39, 16);
		searchPanel.add(lblNbr);

		JLabel lblType = new JLabel("Type:");
		lblType.setBounds(335, 187, 61, 16);
		searchPanel.add(lblType);

		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setBounds(335, 215, 61, 16);
		searchPanel.add(lblLocation);

		JLabel lblProducedTime = new JLabel("Produced:");
		lblProducedTime.setBounds(335, 243, 68, 16);
		searchPanel.add(lblProducedTime);

		JLabel lblDelivered = new JLabel("Delivered:");
		lblDelivered.setBounds(335, 271, 78, 16);
		searchPanel.add(lblDelivered);

		JLabel lblOrderNumber = new JLabel("Order:");
		lblOrderNumber.setBounds(335, 299, 93, 16);
		searchPanel.add(lblOrderNumber);

		searchProduced = new JLabel("");
		searchProduced.setBounds(415, 243, 123, 16);
		searchPanel.add(searchProduced);

		searchDelivered = new JLabel("");
		searchDelivered.setBounds(415, 271, 123, 16);
		searchPanel.add(searchDelivered);

		searchOrder = new JLabel("");
		searchOrder.setBounds(415, 299, 123, 16);
		searchPanel.add(searchOrder);

		searchLocation = new JLabel("");
		searchLocation.setBounds(415, 215, 123, 16);
		searchPanel.add(searchLocation);

		searchType = new JLabel("");
		searchType.setBounds(415, 187, 123, 16);
		searchPanel.add(searchType);

		searchId = new JLabel("");
		searchId.setBounds(415, 159, 123, 16);
		searchPanel.add(searchId);

		searchBtnBlockCurrent = new JButton("Block Current Type");
		searchBtnBlockCurrent.setBounds(384, 327, 172, 29);
		searchPanel.add(searchBtnBlockCurrent);
		
		yearStart = new JComboBox(genYears());
		yearStart.setBounds(349, 427, 90, 27);
		searchPanel.add(yearStart);
		
		monthStart = new JComboBox(months);
		monthStart.setBounds(451, 427, 70, 27);
		searchPanel.add(monthStart);
		
		dayStartModel = new DefaultComboBoxModel();
		for(String day : genDaysOfMonth(JAN, 2011)){
			dayStartModel.addElement(day);
		}
		dayStart = new JComboBox(dayStartModel);
		dayStart.setBounds(533, 427, 70, 27);
		searchPanel.add(dayStart);
		
		yearEnd = new JComboBox(genYears());
		yearEnd.setBounds(349, 501, 90, 27);
		searchPanel.add(yearEnd);
		
		monthEnd = new JComboBox(months);
		monthEnd.setBounds(451, 501, 70, 27);
		searchPanel.add(monthEnd);
		
		dayEndModel = new DefaultComboBoxModel();
		for(String day : genDaysOfMonth(JAN, 2011)){
			dayEndModel.addElement(day);
		}
		dayEnd = new JComboBox(dayEndModel);
		dayEnd.setBounds(533, 501, 70, 27);
		searchPanel.add(dayEnd);
		
		lblBetween = new JLabel("Between:");
		lblBetween.setBounds(439, 390, 61, 16);
		searchPanel.add(lblBetween);
		
		lblAnd = new JLabel("And:");
		lblAnd.setBounds(439, 466, 61, 16);
		searchPanel.add(lblAnd);
		
		lblYear = new JLabel("Year:");
		lblYear.setBounds(357, 409, 61, 16);
		searchPanel.add(lblYear);
		
		label_8 = new JLabel("Year:");
		label_8.setBounds(357, 484, 61, 16);
		searchPanel.add(label_8);
		
		lblMonth = new JLabel("Month:");
		lblMonth.setBounds(460, 409, 61, 16);
		searchPanel.add(lblMonth);
		
		label_9 = new JLabel("Month:");
		label_9.setBounds(460, 484, 61, 16);
		searchPanel.add(label_9);
		
		lblDay = new JLabel("Day:");
		lblDay.setBounds(542, 409, 61, 16);
		searchPanel.add(lblDay);
		
		label_10 = new JLabel("Day:");
		label_10.setBounds(542, 484, 61, 16);
		searchPanel.add(label_10);
		
		btnUnblockCurrentType = new JButton("Unblock Current Type");
		btnUnblockCurrentType.setBounds(384, 367, 172, 23);
		searchPanel.add(btnUnblockCurrentType);

	}
	
	public void setSearchLables(Pallet pallet){
		searchId.setText(pallet.id);
		searchType.setText(pallet.cookieType);
		searchLocation.setText(pallet.location);
		searchProduced.setText(pallet.producedTime);
		searchDelivered.setText(pallet.deliveredTime);
		searchOrder.setText(pallet.orderNbr);
	}
	
	public void setCreateLables(Pallet pallet){
		createId.setText(pallet.id);
		createType.setText(pallet.cookieType);
		createLocation.setText(pallet.location);
		createProduced.setText(pallet.producedTime);
		createDelivered.setText(pallet.deliveredTime);
		createOrder.setText(pallet.orderNbr);
	}
	
	public void addBlockListener(ActionListener blocked) {
		searchBtnBlockCurrent.addActionListener(blocked); 
	}
	
	public void addSearchListener(ActionListener search) {
		btnSearch.addActionListener(search); 
	}
	
	public void addCreateListener(ActionListener create) {
		btnCreatePallet.addActionListener(create); 
	}
	
	public void addSearchSelectionListener(ListSelectionListener selectionListener){
		searchList.addListSelectionListener(selectionListener);
	}
	
	public void addUnBlockedListener(ActionListener unBlock){
		btnUnblockCurrentType.addActionListener(unBlock);
	}
	
	public void addStartItemListener(ItemListener yearListener, ItemListener monthListener){
		yearStart.addItemListener(yearListener);
		monthStart.addItemListener(monthListener);
	}
	
	public void addEndItemListener(ItemListener yearListener, ItemListener monthListener){
		yearEnd.addItemListener(yearListener);
		monthEnd.addItemListener(monthListener);
	}

	public String getSearchField() { 
		return searchField.getText(); 
	}
	
	public String getSelectedPallet(){
		String s = (String) searchList.getSelectedValue();
		if(s == null){
			return null;
		}
		int index = s.indexOf(' ');
		StringBuilder sb = new StringBuilder(s);
		String retString = sb.substring(0, index);
		return retString;
	}
		
	public void updateSearchList(ArrayList<Pallet> palletIds) { 
		searchListModel.clear();
		if(palletIds.size() == 0){
			searchListModel.addElement("Couldn't find a pallet matching your search criteria");
			searchListModel.addElement("please check your search input or contact administator");
		}else{
		for(Pallet nbr : palletIds)
			searchListModel.addElement(nbr.toString());
		}
	}

	public void showFailure(String failType){ 
		JOptionPane.showMessageDialog(null, "Error in database! Could not "+failType+" the specified pallet.", 
				"Failure to "+failType+" pallet", JOptionPane.ERROR_MESSAGE);
	}

	public String getSearchComboboxValue(){
		return attributeBox.getSelectedItem().toString();
	}
	
	public Pallet getCreatedPallet(){
		Pallet newPallet = new Pallet(cookieComboBox.getSelectedItem().toString(), createOrderTextField.getText());
		return newPallet;
	}
	
	public boolean searchBlockedPallets(){
		return rdbtnBlockedPallets.isSelected();
	}

	public String getFromDate() {
		return yearStart.getSelectedItem().toString()+"-"
		+monthStart.getSelectedItem().toString()+"-"+dayStart.getSelectedItem().toString();
	}

	public String getToDate() {
		return yearEnd.getSelectedItem().toString()+"-"
		+monthEnd.getSelectedItem().toString()+"-"+dayEnd.getSelectedItem().toString();
	}
	
	private static String[] genDaysOfMonth(int MONTH, int year) {
		int nbrOfDays;
		if (MONTH == APR || MONTH == JUN || MONTH == SEP || MONTH == NOV)
			nbrOfDays = 30;
		else if (MONTH == FEB) {
			nbrOfDays = 28;
			if ((2012 - year) % 4 == 0)
				nbrOfDays++;
		} else {
			nbrOfDays = 31;
		}
		String[] days = new String[nbrOfDays];
		for (int i = 1; i <= nbrOfDays; i++) {
			String day = i + "";
			if (i < 10) {
				day = "0" + day;
			}
			days[i-1] = day;
		}
		return days;
	}
	
	private static String[] genYears(){
		String[] years = new String[51];
		for(int i= 0; i<51; i++){
			years[i] = (2000+i)+"";
		}
		return years;
	}
	
	public void updateStartDayComboBox(){
		dayStartModel.removeAllElements();
		String month = (String)monthStart.getSelectedItem();
		if(month.startsWith("0"))
			month = month.substring(1);
		String[] days = genDaysOfMonth(Integer.parseInt(month),
				Integer.parseInt((String)yearStart.getSelectedItem())); 
		for(String day : days){
			dayStartModel.addElement(day);
		}
	}
	
	public void updateEndDayComboBox(){
		dayEndModel.removeAllElements();
		String month = (String)monthEnd.getSelectedItem();
		if(month.startsWith("0"))
			month = month.substring(1);
		String[] days = genDaysOfMonth(Integer.parseInt(month),
				Integer.parseInt((String)yearEnd.getSelectedItem())); 
		for(String day : days){
			dayEndModel.addElement(day);
		}
	}
	
	public void updateOrderComboBox(String[] orders){
		orderModel.removeAllElements();
		for(String order : orders){
			orderModel.addElement(order);
		}
	}
}
